package pos.data;

import pos.domain.*;

import pos.utils.UIDGenerator;

import java.sql.*;

public class JDBCPropertyRatingDAO implements IPropertyRatingDAO {

	IUserRatingDAO urdao=new JDBCUserRatingDAO();
	
	public void insert(PropertyRating pt, UserRating ur) {
		Connection conn=ConnectionManager.getInstance().checkOut();
		PreparedStatement stmt=null;
        UIDGenerator propertyRatingOID=UIDGenerator.getInstance();
        boolean rated = true;
        String sql="INSERT INTO PropertyRating (propertyRatingOID,exchangeID,propertyID,rated,geoLocation,cleaning,contracted,coments) VALUES (?, ?, ?, ?, ?, ?, ?, ?)";
        try{
        	stmt=conn.prepareStatement(sql);
        	stmt.setString(1, propertyRatingOID.getKey());
        	stmt.setString(2, pt.getExchangeID());
        	stmt.setString(3,pt.getPropertyID());
        	stmt.setBoolean(4, rated);
        	stmt.setInt(5, pt.getGeographicalLocation());
        	stmt.setInt(6, pt.getCleaning());
        	stmt.setInt(7, pt.getContracted());
        	stmt.setString(8, pt.getComments());
        	
        	stmt.executeUpdate();
        	//insertamos la valoracion del usuario
        	urdao.insertUserRating(conn, ur);
        	
        } catch (SQLException e) {
            System.err.println("Message: " + e.getMessage());
            System.err.println("SQLState: " + e.getSQLState());
            System.err.println("ErrorCode: " + e.getErrorCode());
        } finally {
        	ConnectionManager.getInstance().checkIn(conn);
            try {
                if (stmt != null)
                    stmt.close();
            } catch (SQLException e) {
            }
        }
        
	}

}
